﻿using System;
using System.Linq;

using Nop.Core.Data;
using Nop.Core.Infrastructure;
using Nop.Core.Infrastructure.DependencyManagement;
using Nop.Core.Plugins;
using Nop.Data;
using Nop.Services.Media;

using Autofac;
using Autofac.Core;
using Autofac.Integration.Mvc;

using RonsHouse.Plugin.Misc.AlternativeStorage.Services;

namespace RonsHouse.Plugin.Misc.AlternativeStorage
{
	public class DependencyRegistrar : IDependencyRegistrar
	{
		public void Register(ContainerBuilder builder, ITypeFinder typeFinder)
		{
			var types = typeFinder.FindClassesOfType<IPluginFinder>(true);
			if (types.Count() == 1)
			{
				var plugins = Activator.CreateInstance(types.First()) as IPluginFinder;
				var desc = plugins.GetPluginDescriptorBySystemName("RonsHouse.Misc.AlternativeStorage");
				if (desc == null || desc.Installed == false)
					return;
			}

			builder.RegisterType<AlternativeStorageDownloadService>().As<IDownloadService>().InstancePerRequest();
		}

		public int Order
		{
			get { return 0; }
		}
	}
}